package com.bjx.flow.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bjx.api.flow.domain.vo.FlowNodeVO;
import com.bjx.api.flow.domain.vo.FlowStartVO;
import com.bjx.common.core.domain.R;
import com.bjx.flow.domain.query.FlowInstanceQuery;
import com.bjx.flow.domain.query.FlowNodeQuery;
import com.bjx.flow.domain.vo.FlowInstanceVO;
import com.bjx.flow.service.IBaseFlowService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.camunda.bpm.engine.runtime.ProcessInstance;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * @desc: 基础流程定义
 * @author: liushuai
 * @date: 2022/10/6 15:53
 */
@RequestMapping("/base")
@Tag(name = "基础流程控制器")
@RestController
public class BaseFlowController {
    @Resource
    private IBaseFlowService baseFlowService;

    /**
     * 启动流程
     * @param flowStartVO
     * @return: com.bjx.common.core.domain.R
     * @Author: liushuai
     * @Date: 2022/11/1 16:26
     */
    @PostMapping("/startFlow")
    @Operation(summary = "启动流程")
    public R<String> startFlow(@RequestBody @Validated FlowStartVO flowStartVO){
        ProcessInstance processInstance = baseFlowService.startFlow(flowStartVO);
       return R.success(processInstance.getProcessInstanceId());
    }


    /**
     * 根据流程定义Id获取第一个审批节点信息
     * @param definitionId
     * @return: com.bjx.common.core.domain.R
     * @Author: liushuai
     * @Date: 2022/11/17 21:12
     */
    @GetMapping("/selectFirstFlowNodeInfoByDefinitionId")
    @Operation(summary = "根据流程定义Id获取第一个审批节点信息")
    public R<FlowNodeVO> selectFirstFlowNodeInfoByDefinitionId(@RequestParam("definitionId") String definitionId){
       FlowNodeVO vo = baseFlowService.selectFirstFlowNodeInfoByDefinitionId(definitionId);
       return  R.success(vo);
    }

    /**
     * 根据流程定义Id获取第一个审批节点信息
     * @param definitionKey
     * @return: com.bjx.common.core.domain.R
     * @Author: liushuai
     * @Date: 2022/11/17 21:12
     */
    @GetMapping("/selectFirstFlowNodeInfoByDefinitionKey")
    @Operation(summary = "根据流程定义Key获取第一个审批节点信息")
    public R<FlowNodeVO> selectFirstFlowNodeInfoByDefinitionKey(@RequestParam("definitionKey") String definitionKey){
        FlowNodeVO vo = baseFlowService.selectFirstFlowNodeInfoByDefinitionKey(definitionKey);
        return  R.success(vo);
    }

    /**
     * 根据当前用户id分页获取待办信息
     * @param
     * @return: com.bjx.common.core.domain.R
     * @Author: liushuai
     * @Date: 2022/12/24 18:35
     */
    @GetMapping("listTodoTaskPageByCurrentUser")
    @Operation(summary = "根据当前用户id分页获取待办信息")
    public R listTodoTaskPageByCurrentUser(FlowInstanceQuery flowInstanceQuery){
        IPage<FlowInstanceVO> instanceVOIPage = baseFlowService.listTodoTaskByCurrentUser(flowInstanceQuery);
        return R.success(instanceVOIPage);
    }

    /**
     * 获取下一个节点审批任务
     * @param
     * @return: com.bjx.common.core.domain.R
     * @Author: liushuai
     * @Date: 2023/1/4 13:59
     */
    @PostMapping("getNextTaskNode")
    @Operation(summary = "获取下一个节点审批任务")
    public R<List<FlowNodeVO>> getNextTaskNode(@RequestBody FlowNodeQuery flowNodeQuery){
        List<FlowNodeVO> flowNodeVOS = baseFlowService.getNextTaskNode(flowNodeQuery);
        return  R.success(flowNodeVOS);
    }

}
